﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SharpAnalyzer.Entities;
using SharpAnalyzer.Utilities;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SharpAnalyzer.Analyzers
{
    internal sealed class LogicalComplexityAnalyzer : CSharpSyntaxWalker
    {
        private int counter;

        public LogicalComplexityAnalyzer()
          : base(SyntaxWalkerDepth.Node)
        {
        }

        public int Calculate(Member node)
        {
            Verify.NotNull(() => node);

            BlockSyntax body = MemberBodySelector.FindBody(node);
            if (body != null)
                Visit(body);
            return counter;
        }

        public override void VisitBinaryExpression(BinaryExpressionSyntax node)
        {
            base.VisitBinaryExpression(node);
            switch (node.Kind())
            {
                case SyntaxKind.LogicalOrExpression:
                case SyntaxKind.LogicalAndExpression:
                case SyntaxKind.LogicalNotExpression:
                    checked { ++counter; }
                    break;
            }
        }
    }
}
